package day_2022_7_to_8.day48;

/**
 * @author haomin
 * @date 2022/08/30 20:21
 **/
public class Day45_13_Offer20_IsNumber {
    class Solution {
        public boolean isNumber(String s) {
            boolean hasE = false, hasNum = false, isFloat = false;
            s = s.trim();
            char[] ch = s.toCharArray();
            for(int i = 0; i < ch.length; ++i){
                char c = ch[i];
                if((c == '+' || c == '-')
                        && ((i == 0) || ((i > 0) && (ch[i-1] == 'e' || ch[i-1] == 'E')))){
                }else if((c == 'e' || c == 'E') && !hasE && hasNum){
                    hasE = true;
                    hasNum = false;
                }else if(c == '.' && !hasE && !isFloat){
                    isFloat = true;
                }else if(Character.isDigit(c)){
                    hasNum = true;
                }else {
                    return false;
                }
            }
            return hasNum;
        }
    }
    class Solution2 {
        public boolean isNumber(String s) {
            s = s.trim();
            try {
                Double.valueOf(s);
                if(s.charAt(s.length()-1) == 'f' || s.charAt(s.length()-1) == 'D' || s.charAt(s.length()-1) == 'y'){
                    return false;
                }
                return true;
            }catch (Exception e){
                return false;
            }
        }
    }
}